<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
 * ============================================================================
 * $Author: 当燃   2016-05-10
 */ 
namespace Mobile\Controller;
use Mobile\Logic\ActivityLogic;

class ActivityController extends MobileBaseController {
    public function index(){      
        $this->display();
    }
   /**
    * 商品详情页
    */ 
    public function group(){
        //form表单提交
        C('TOKEN_ON',true);  
        $goodsLogic = new \Home\Logic\GoodsLogic();
        $goods_id = I("get.id",66);
        
        $group_buy_info = M('GroupBuy')->where("goods_id = $goods_id and ".time()." >= start_time and ".time()." <= end_time ")->find(); // 找出这个商品
        if(empty($group_buy_info)) 
        {
            //$this->error("此商品没有团购活动",U('Home/Goods/goodsInfo',array('id'=>$goods_id)));
        }
                    
        $goods = M('Goods')->where("goods_id = $goods_id")->find();
        $goods_images_list = M('GoodsImages')->where("goods_id = $goods_id")->select(); // 商品 图册
                
        $goods_attribute = M('GoodsAttribute')->getField('attr_id,attr_name'); // 查询属性
        $goods_attr_list = M('GoodsAttr')->where("goods_id = $goods_id")->select(); // 查询商品属性表
                        
        $Model = new \Think\Model();        
        // 商品规格 价钱 库存表 找出 所有 规格项id
        $keys = M('SpecGoodsPrice')->where("goods_id = $goods_id")->getField("GROUP_CONCAT(`key` SEPARATOR '_') ");         
        if($keys)
        {
             $specImage =  M('SpecImage')->where("goods_id = $goods_id and src != '' ")->getField("spec_image_id,src");// 规格对应的 图片表， 例如颜色                
             $keys = str_replace('_',',',$keys);             
             $sql  = "SELECT a.name,a.order,b.* FROM __PREFIX__spec AS a INNER JOIN __PREFIX__spec_item AS b ON a.id = b.spec_id WHERE b.id IN($keys) ORDER BY a.order";
             $filter_spec2 = $Model->query($sql);             
             foreach($filter_spec2 as $key => $val)
             {                                  
                 $filter_spec[$val['name']][] = array(
                     'item_id'=> $val['id'],
                     'item'=> $val['item'],
                     'src'=>$specImage[$val['id']],
                     );                 
             }            
        }                
        $spec_goods_price  = M('spec_goods_price')->where("goods_id = $goods_id")->getField("key,price,store_count"); // 规格 对应 价格 库存表
        M('Goods')->where("goods_id=$goods_id")->save(array('click_count'=>$goods['click_count']+1 )); // 统计点击数
        $commentStatistics = $goodsLogic->commentStatistics($goods_id);// 获取某个商品的评论统计           
        $this->assign('group_buy_info',$group_buy_info);
        $this->assign('spec_goods_price', json_encode($spec_goods_price,true)); // 规格 对应 价格 库存表
        $this->assign('commentStatistics',$commentStatistics);
        $this->assign('goods_attribute',$goods_attribute);       
        $this->assign('goods_attr_list',$goods_attr_list);
        $this->assign('filter_spec',$filter_spec);
        $this->assign('goods_images_list',$goods_images_list);
        $this->assign('goods',$goods);
        $this->display();
    } 
    
	//活动列表
    public function activityList(){ 
        $filter_param = array(); // 帅选数组
    	$activity_id = I('activity_id'); // 排序
        $filter_param['activity_id'] = $activity_id; //加入帅选条件中  
		
    	 if(empty($activity_id)) 
        {
            $this->error("活动正在策划中,敬请期待",U('Mobile/Goods/goodsList'));
            exit;
        }
		
		$now = time();
        $where = " end_time>$now and id =$activity_id ";
        $activity = M('activity')->where($where)->find();
        if(empty($activity)) 
        {
            $this->error("活动已结束,看下别的商品吧",U('Mobile/Goods/goodsList'));
            exit;
        }
		
		
        $sort = I('get.sort','goods_id'); // 排序
        $sort_asc = I('get.sort_asc','asc'); // 排序 


		if($activity[coupon_id])
		{
        	$coupon_list = M('coupon')->where("id in (".$activity[coupon_id].")")->select();
		}
		
		if($activity[m_banner_id])
		{
        	$banner_list = explode("|",$activity[m_banner_id]);
		}
		
		
		
         
    	$goodsLogic = new \Home\Logic\GoodsLogic(); // 前台商品操作逻辑类
    	 
    	$filter_goods_id = M('goods')->where("is_on_sale=1 and goods_id in ($activity[goods_id])")->cache(true)->getField("goods_id",true);
   
    	$count = count($filter_goods_id);
    	//$page = new Page($count,4);
    	if($count > 0)
    	{
    		$goods_list = M('goods')->where("goods_id in (".  implode(',', $filter_goods_id).")")->order("$sort $sort_asc")->limit($page->firstRow.','.$page->listRows)->select();
    		$filter_goods_id2 = get_arr_column($goods_list, 'goods_id');
    		if($filter_goods_id2)
    			$goods_images = M('goods_images')->where("goods_id in (".  implode(',', $filter_goods_id2).")")->cache(true)->select();
    	}
    	
    	$this->assign('goods_list',$goods_list);
    	
        $this->assign('banner_list',$banner_list);  
        $this->assign('coupon_list',$coupon_list);  
		
    	$this->assign('goods_images',$goods_images);  // 相册图片
    	
    	$this->assign('filter_param',$filter_param); // 帅选条件

    	//$this->assign('page',$page);// 赋值分页输出
    	C('TOKEN_ON',false);
        
        if($_GET['is_ajax'])
            $this->display('ajaxactivityList');
        else
            $this->display();
        
    }    
	
	
	public function ajaxactivityList() {
        $where ='';

        $cat_id  = I("id",0); // 所选择的商品分类id
        if($cat_id > 0)
        {
            $grandson_ids = getCatGrandson($cat_id);
            $where .= " WHERE cat_id in(".  implode(',', $grandson_ids).") and goods_id in (42,41) "; // 初始化搜索条件
        }

        $Model  = new \Think\Model();
        $result = $Model->query("select count(1) as count from __PREFIX__goods $where ");
        $count = $result[0]['count'];
        $page = new AjaxPage($count,10);

        $order = " order by goods_id desc"; // 排序
        $limit = " limit ".$page->firstRow.','.$page->listRows;
        $list = $Model->query("select *  from __PREFIX__goods $where $order $limit");

        $this->assign('lists',$list);
        $html = $this->fetch('ajaxGoodsList'); //$this->display('ajax_goods_list');
       exit($html);
    }
	
    
    /**
     * 团购活动列表
     */
    public function group_list()
    {
    	$count =  M('GroupBuy')->where(time()." >= start_time and ".time()." <= end_time ")->count();// 查询满足要求的总记录数
    	$Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数  	
    	$show = $Page->show();// 分页显示输出
    	$this->assign('page',$show);// 赋值分页输出
        $list = M('GroupBuy')->where(time()." >= start_time and ".time()." <= end_time ")->limit($Page->firstRow.','.$Page->listRows)->select(); // 找出这个商品        
        $this->assign('list', $list);
        $this->display();
    }
	
	 public function coupon_list()
    {
        $atype = I('atype', 1);
        $p = I('p', '');
       // $activityLogic = new ActivityLogic();
		
       $CouponLogic = new \Mobile\Logic\CouponLogic();
        $result = $CouponLogic->getCouponList($atype, $user['user_id'], $p);
		print_r($result);
        $this->assign('coupon_list', $result);
		  if($_GET['is_ajax']){
            $this->display('ajax_coupon_list');
        }
        $this->display();
    }
}